package lhc.alg.top100;

/**
 * description: https://leetcode.cn/problems/number-of-islands/
 *  岛屿数量
 * author: hongchen.liao
 * date:  2022/9/23
 */
public class _200_ {

    class Solution {
        int m, n;
        public int numIslands(char[][] grid) {
            m = grid.length;
            n = grid[0].length;
            int numIslands = 0;
            for(int i = 0; i < m; i++){
                for(int j = 0; j < n; j++){
                    if(grid[i][j] == '1'){
                        dfs(grid, i, j);
                        numIslands++;
                    }
                }
            }
            return numIslands;
        }

        void dfs(char[][] grid, int r, int c){
            if(r < 0 || r >= m || c < 0 || c >= n || grid[r][c] == '0'){
                return;
            }
            grid[r][c] = '0';
            dfs(grid, r+1, c);
            dfs(grid, r-1, c);
            dfs(grid, r, c+1);
            dfs(grid, r, c-1);
        }
    }

}
